package com.fanrui.code.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.fanrui.code.entity.FamilyList;
import com.fanrui.code.entity.FamilyListMapper;
import com.fanrui.code.util.SessionUtil;

public class FamilyListDao {

	// singleton
		private FamilyListDao() {
		}

		private static class FamilyListDaoSingleton {
			private static FamilyListDao instance = new FamilyListDao();
		}

		public static FamilyListDao getInstance() {
			return FamilyListDaoSingleton.instance;
		}
	
		
		
	/**
	 * 插入数据
	 * */
	public void insert(FamilyList list){
		try (SqlSession session = SessionUtil.getSession()){
			if(list != null){
				FamilyListMapper mapper = session.getMapper(FamilyListMapper.class);
				mapper.insert(list);
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void delete(Long playerId, Integer clubId){
		try (SqlSession session = SessionUtil.getSession()){
			FamilyListMapper mapper = session.getMapper(FamilyListMapper.class);
			mapper.deleteByClubId(playerId, clubId);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询俱乐部的所有成员
	 * @param clubId
	 * @return
	 */
	public List<FamilyList> getFamilyMember(Integer clubId){
		try (SqlSession session = SessionUtil.getSession()){
			FamilyListMapper mapper = session.getMapper(FamilyListMapper.class);
			return mapper.selectByClubId(clubId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新俱乐部所有成员
	 */
//	public void updateClubMember(List<FamilyList> members, Integer clubId) {
//		if(members != null && !members.isEmpty()) {
//			SqlSession session = SessionUtil.getSessionFactory().openSession(false);
//			try {
//				FamilyListMapper mapper = session.getMapper(FamilyListMapper.class);
//				mapper.deleteByClubId(clubId);
//				for(FamilyList fl : members) {
//					mapper.insert(fl);
//				}
//				session.commit();
//			} catch (Exception e) {
//				session.rollback();
//				e.printStackTrace();
//			} finally {
//				session.close();
//			}
//		}
//	}
	
	/**
	 * 查询玩家加入的所有俱乐部
	 * @param playerId
	 * @return
	 */
	public List<FamilyList> queryPlayersClub(Long playerId){
		try (SqlSession session = SessionUtil.getSession()){
			FamilyListMapper mapper = session.getMapper(FamilyListMapper.class);
			return mapper.queryPlayersClub(playerId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
